package com.stockz.ui.swing.charting.display.indicator;

import java.awt.Graphics2D;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.stockz.core.model.Bar;
import com.stockz.core.util.BarUtil;
import com.stockz.core.util.BarUtil.BarValueType;
import com.stockz.core.util.ListUtil;
import com.stockz.ui.swing.charting.painter.indicator.LineIndicatorPainter;
import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.util.UIChartingConstant.OVERLAY;

@Component
@Scope("prototype")
public class VolumeSum extends
		AbstractDisplayableIndicatorConfiguration{

	public static class Configuration{
		private int span = 2;

		public int getSpan() {
			return span;
		}

		public void setSpan(int span) {
			this.span = span;
		}
		
	}
	
	private Configuration configuration = new Configuration();
	
	@Override
	public String getFullName() {
		return "Volume Sum";
	}

	@Override
	public String getShortName() {
		return "VS";
	}

	@Override
	public String getDescription() {
		return getFullName();
	}

	@Override
	public OVERLAY getOverlay() {
		return OVERLAY.INDEPENDENT;
	}

	@Override
	public boolean isOverlappable() {
		return true;
	}

	@Override
	public void paint(Graphics2D g) {
		LineIndicatorPainter.paint(g, this);
	}

	public static double[] values(List<Bar> barList, Configuration configuration, BarValueType barValueType) {
		if(barList != null && barList.size() > configuration.getSpan()){
			double[] values = new double[barList.size()];
			for(int i = barList.size()-1; i >= configuration.getSpan(); i--){
				for(int j = barList.size()-1; j >= i+configuration.getSpan(); j--){
					double avg = MathUtil.avg(ListUtil.convert(barList, barValueType, j-configuration.getSpan(), j-1));
					double value = BarUtil.getValue(barList.get(j), barValueType);
					if(avg >= value){
						values[i] += barList.get(j).getVolume();
					}else{
						values[i] -= barList.get(j).getVolume();
					}
				}
			}
			return values;
		}
		return null;
	}

	@Override
	public Object getConfiguration() {
		return configuration;
	}

	@Override
	public void refreshValues() {
		setValues(values(getStockChartPanel().getStockChartPanelState().getBarList(),
				configuration,getBarValueType()));
	}
	
}
